/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.controllers;


import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import edu.ieslaferreria.creditsintesis.logicgame.ActionStates;
import edu.ieslaferreria.creditsintesis.logicgame.Player;
import edu.ieslaferreria.creditsintesis.logicgame.statemachineimpl.PlayerStateMachine;


/**
 *
 * @author Josep / Carlos
 */
public class UserActionController{


    private ActionStates stateObjects;
    private Player player;
    private InputHandler input;

    /**
     * Supply the node to control and the api that will handle input creation.
     * @param node the node we wish to move
     * @param api the library that will handle creation of the input.
     */
    public UserActionController(String api, ActionStates stateObj, InputHandler input) {
        this.stateObjects = stateObj;
        this.input = input;
        setKeyBindings(api);
        setActions();
    }

    public UserActionController(Player play, String api, ActionStates stateObj, InputHandler input) {
        this.stateObjects = stateObj;
        this.player = play;
        this.input = input;
        setKeyBindings(api);
        setActions();
    }

    /**
     * creates the keyboard object, allowing us to obtain the values of a keyboard as keys are
     * pressed. It then sets the actions to be triggered based on if certain keys are pressed (WSAD).
     * @param api
     */
    private void setKeyBindings(String api) {
        KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();

        keyboard.set("forward", KeyInput.KEY_UP);
        keyboard.set("backward", KeyInput.KEY_DOWN);
        keyboard.set("turnRight", KeyInput.KEY_RIGHT);
        keyboard.set("turnLeft", KeyInput.KEY_LEFT);

    }

    /**
     * assigns action classes to triggers. These actions handle moving the node forward, backward and
     * rotating it.
     * @param node the node to control.
     */
    private void setActions() {
        PlayerTransitionActivator toForward = new PlayerTransitionActivator(PlayerStateMachine.TR_GO_FORWARD);
        getInput().addAction(toForward, "forward", true);

        PlayerTransitionActivator toBackward = new PlayerTransitionActivator(PlayerStateMachine.TR_GO_BACK);
        getInput().addAction(toBackward, "backward", true);

        PlayerTransitionActivator toRotateRight = new PlayerTransitionActivator(PlayerStateMachine.TR_SPIN_RIGHT);
        getInput().addAction(toRotateRight, "turnRight", true);

        PlayerTransitionActivator toRotateLeft = new PlayerTransitionActivator(PlayerStateMachine.TR_SPIN_LEFT);
        getInput().addAction(toRotateLeft, "turnLeft", true);
    }

        /**
     * @return the stateObjects
     */
    public ActionStates getStateObjects() {
        return stateObjects;
    }

    /**
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * @param player the player to set
     */
    public void setPlayer(Player player) {
        this.player = player;
    }

    /**
     * @return the input
     */
    public InputHandler getInput() {
        return input;
    }


    private class PlayerTransitionActivator extends InputAction{
        private int transitionToProcess;

        public PlayerTransitionActivator(int transitionToProcess){
            this.transitionToProcess = transitionToProcess;
        }

        public void performAction(InputActionEvent iae) {
            //System.out.print("Comanda: " + iae.getTriggerName() + ". Transiton: " +this.transitionToProcess);
            //System.out.print(" Old state: " + player.getToDo());
            player.processTransition(this.transitionToProcess);
            //System.out.println(" New state: " + player.getToDo());
//            stateObjects.getObjectsToValidateState().add(player);
        }

    }


}